/**
 * 创建CrudSchemas代码
 * @param fieldData
 * @returns
 */
export const createCrudSchemas = (fieldData: any) => {
  let crudSchemas = '['
  let num = 0
  fieldData.forEach((item) => {
    let newData = '{'
    newData += "field:'" + item.column_name + "'"
    newData += ",label:'" + item.column_comment + "'"
    if (item.fixed != null && item.fixed != '') {
      newData += ",fixed:'" + item.fixed + "'"
    }
    if (item.width != null && item.width != '') {
      newData += ',width:' + item.width
    }

    // 字典处理
    if (item.attr_type === 'dict') {
      if (item.dict_type != null) {
        newData +=
          ",formatter:(_: Recordable, __: TableColumn, cellValue: number | string) => {return getDictLabel('" +
          item.dict_type +
          "', cellValue)}"
      } else {
        alert('字典类型dict_type不能为空')
        return ''
      }
    }

    // 处理table start
    newData += ',table: {'
    // 处理是否显示
    if (item.table_show != null && item.table_show == true) {
      newData += 'show:' + item.table_show
    } else {
      newData += 'show:false'
    }
    newData += '}'
    // 处理table end

    // 处理search start
    newData += ',search: {'
    // 处理是否显示
    if (item.search_show != null && item.search_show == true) {
      newData += 'show:' + item.search_show
      // 字典处理
      if (item.attr_type === 'dict') {
        newData += ",component:'Select'"
        newData += ',componentProps:{options: dict.' + item.dict_type + '}'
      }
    } else {
      newData += 'show:false'
    }
    newData += '}'
    // 处理search end

    // 处理form start
    newData += ',form: {'
    // 处理是否显示
    if (item.form_show != null && item.form_show == true) {
      newData += 'show:' + item.form_show
      // 字典处理
      if (item.attr_type === 'dict') {
        newData += ",component:'Select'"
        newData += ',componentProps:{options: dict.' + item.dict_type + '}'
      }
      // 日期
      if (item.attr_type === 'date') {
        newData += ",component:'DatePicker'"
        newData += ",componentProps:{disabled: true,type: 'date',valueFormat: 'YYYY-MM-DD'}"
      }
      // 日期时间
      if (item.attr_type === 'datetime') {
        newData += ",component:'DatePicker'"
        newData +=
          ",componentProps:{disabled: true,type: 'datetime',valueFormat: 'YYYY-MM-DD HH:mm:ss'}"
      }
      // 参照
      if (item.attr_type === 'refer') {
        newData += ",component:'DialogRefer'"
        newData += ',componentProps:{'
        if (item.getListApi != null) {
          newData += ",getListApi:'" + item.getListApi + "'"
        } else {
          alert('参照类型getListApi参数不能为空')
          return ''
        }
        newData += ",response:{count: 'count',data: 'data'}"
        newData += ",title:'" + item.refer_title + "'"
        newData += ",nameField:'" + item.nameField + "'"
        newData += ",valueField:'" + item.valueField + "'"
        newData += ',crudSchemas:' + item.crudSchemas + ''
        newData += '}'
      }
    } else {
      newData += 'show:false'
    }
    newData += '}'
    // 处理form end

    // 处理detail start
    newData += ',detail: {'
    // 处理是否显示
    if (item.detail_show != null && item.form_show == true) {
      newData += 'show:' + item.detail_show
    } else {
      newData += 'show:false'
    }
    newData += '}'
    // 处理detail end

    newData += '}'

    if (num == 0) {
      crudSchemas += newData
    } else {
      crudSchemas += ',' + newData
    }
    num++
  })

  // 最后添加一个操作列
  crudSchemas +=
    ",{field: 'action',width: 300,fixed: 'right',label: '操作',form: {show: false},detail: {show: false}}"
  crudSchemas += ']'
  return crudSchemas
}

/**
 * 创建FormSchemas代码
 * @param fieldData
 * @returns
 */
export const createFormSchemas = (fieldData: any) => {
  let crudSchemas = '['
  let num = 0
  fieldData.forEach((item) => {
    let newData = '{'
    newData += "field:'" + item.column_name + "'"
    newData += ",label:'" + item.column_comment + "'"
    newData += getFormComponent(item)
    newData += '}'

    if (num == 0) {
      crudSchemas += newData
    } else {
      crudSchemas += ',' + newData
    }
    num++
  })
  crudSchemas += ']'
  return crudSchemas
}

const getFormComponent = (item) => {
  let newData = ''
  switch (item.attr_type) {
    case 'dict': // 字典处理
      newData += ",component:'Select'"
      newData += ',componentProps:{options: dict.' + item.dict_type + '}'
      break
    case 'TreeSelect': // 字典处理
      newData += ",component:'TreeSelect'"
      newData +=
        ',componentProps:{data: ' +
        item.data +
        ",'node-key': '" +
        item.valueField +
        "','check-strictly': true,'default-expand-all': true,props: {children: '" +
        item.childrenField +
        "',label: '" +
        item.nameField +
        "'}}"
      break
    case 'date': // 日期
      newData += ",component:'DatePicker'"
      newData += ",componentProps:{disabled: true,type: 'date',valueFormat: 'YYYY-MM-DD'}"
      break
    case 'datetime': // 日期时间
      newData += ",component:'DatePicker'"
      newData +=
        ",componentProps:{disabled: true,type: 'datetime',valueFormat: 'YYYY-MM-DD HH:mm:ss'}"
      break
    case 'refer': // 参照处理
      newData += ",component:'DialogRefer'"
      newData += ',componentProps:{'
      if (item.getListApi != null) {
        newData += ",getListApi:'" + item.getListApi + "'"
      } else {
        alert('参照类型getListApi参数不能为空')
        return ''
      }
      newData += ",response:{count: 'count',data: 'data'}"
      newData += ",title:'" + item.refer_title + "'"
      newData += ",nameField:'" + item.nameField + "'"
      newData += ",valueField:'" + item.valueField + "'"
      newData += ',crudSchemas:' + item.crudSchemas + ''
      newData += '}'
      break
    case 'text':
    default:
      newData += ",component:'Input'" // 默认是文本框
  }
  return newData
}
